﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.WebPages;

namespace Ruandao.MiniFace
{
    public delegate string GetCurrrentThemeName();

    public class ThemeHelper : HelperPage
    {

        /// <summary>
        /// 仅仅引入某个分组下的部分皮肤组件。
        /// </summary>
        /// <param name="theme"></param>
        /// <param name="groupName"></param>
        /// <param name="components"></param>
        /// <returns></returns>
        static public HelperResult Reference(Theme theme, string groupName, params string[] components )
        {
            foreach (string component in theme.Groups[groupName])
            {
                foreach (List<dynamic> list in theme.Components[component])
                {
                    WriteSkinFiles(list);
                }
            }

            foreach (string component in components)
            {
                foreach (List<dynamic> list in theme.Components[component])
                {
                    WriteSkinFiles(list);
                }
            }

            //return new HelperResult(writer => writer.Write(sb.ToString()));
            return new HelperResult(writer => { });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list">列表的元素类型包括三个属性：Type="js|css"，File=""，MiniFile=""</param>
        /// <returns></returns>
        static private void WriteSkinFiles(IList<dynamic> list)
        {
            StringBuilder sb = new StringBuilder();
            foreach (dynamic item in list)
            {
                string file = item.File;
#if(!DEBUG)
                  if( !string.IsNullOrEmpty(item.MiniFile))
                  {
                      file = item.MiniFile;
                  }
#endif
                // sb.Append("<link href='" + theme.BaseSkinsUrl + "/yaml/base.css' rel='stylesheet' type='text/css' />");
                // sb.Append("<link href='" + theme.BaseSkinsUrl + "/Rd_Skins/yaml/iehacks.css' rel='stylesheet' type='text/css' />");
                string html = "{{$}}";
                if (item.SpecialBrowser == "ie<7")
                {
                    html = "<!--[if lte IE 7]>{{$}}<![endif]-->";
                }
                if (item.Type == "js")
                {
                    html = html.Replace("{{$}}",
                        string.Format("\n<script src=\"{0}\" type=\"text/javascript\" ></script>", file));
                }
                if (item.Type == "css")
                {
                    html = html.Replace("{{$}}",
                        string.Format("\n<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />", file));
                }
                sb.Append(html);
            }
            Html.Raw( sb.ToString() );
        }
    }

}
